#!/usr/bin/env bash
set -e

LOG_FILE="/root/result.log"
cur_path=$(pwd)

echo "start to prepare coverage env"
chmod +x /root/repo/ci-repos/.common_scripts/common.sh
source /root/repo/ci-repos/.common_scripts/common.sh
set_gitee_hosts
prepare_git $1 $2
prepare_conan2 $3 $4
prepare_bingo
prepare_sdk

echo "start to test coverage.........."
cd ${cur_path}
echo "==========timeout 45m bingo test -ut -it -cov -jit =================="
timeout 45m bingo test -ut -it -cov -jit | tee ${LOG_FILE} 2>&1 || die
if [ $? -gt 0 ]; then
    die "DT任务失败"
    exit 1
fi

CNT=$(grep -cE '\(=== test failed ===|lua call \[.*\] error' ${LOG_FILE}) ||:
if [ ${CNT} -gt 0 ]; then
  die "IT失败, 请在日志中检索==== test failed ======或者lua call .* error 查看详细日志"
  exit 1
fi

dt_result="temp/coverage/dt_result.json"
if [ -f "${dt_result}" ]; then
    cat ${dt_result}
    python3 /root/repo/ci-repos/.common_scripts/coverage_checker.py ${dt_result}
    exit_code=$?
else
    die "DT任务失败, 未生成${dt_result}"
    exit 1
fi